package com.nfit.shopadmin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.nfit.shopadmin.mapper.AddressMapper;
import com.nfit.shopadmin.service.AddressService;
import com.nfit.shopmodel.dto.address.AddressDto;
import com.nfit.shopmodel.entity.address.Address;
import com.nfit.shopmodel.vo.address.AddressVo;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:AddressServiceImpl
 * Package:com.nfit.shopadmin.service.impl
 * Description
 *
 * @Author L
 * @Create 2024/12/29 1:49
 */
@Schema(description = "地址业务处理")
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;


    /**
     * 根据userId查询地址列表
     */
    @Override
    public List<AddressVo> findByUserId(Long userId) {
        List<Address> list = addressMapper.findByUserId(userId);
        //封装数据
        List<AddressVo> addressVoList = list.stream().map(a -> {
            AddressVo addressVo = new AddressVo();
            addressVo.setId(a.getId());
            addressVo.setName(a.getName());
            addressVo.setPhone(a.getPhone());
            addressVo.setAddress(a.getAddress());
            addressVo.setFullAddress(a.getFullAddress());
            addressVo.setIsDefault(a.getIsDefault());
            return addressVo;
        }).collect(Collectors.toList());
        return addressVoList;
    }

    /**
     * 根据ID查询地址详情信息
     */
    @Override
    public AddressVo findById(Long id) {
        Address address = addressMapper.findById(id);
        // 创建目标对象
        AddressVo addressVo = new AddressVo();
        // 方法复制属性
        BeanUtil.copyProperties(address, addressVo);
        return addressVo;
    }

    /**
     * 修改地址
     */
    @Override
    public void update(AddressDto addressDto,Long id) {
        addressMapper.updateAddressById(addressDto,id);
    }

    /**
     * 添加地址
     */
    @Override
    public void save(AddressDto addressDto) {
        addressMapper.saveAddress(addressDto);
    }

    @Override
    public void delete(Long id) {
        addressMapper.delete(id);
    }
}
